
class CConnection
{
public:

    typedef enum
    {
        CONNECTION_STATE_RESET,
        CONNECTION_STATE_INIT,
        CONNECTION_STATE_BIND_OK,
        CONNECTION_STATE_ACCEPT_OK,
    } CONNECTION_STATE;

    CConnection();
    ~CConnection();

    void init(int port, char* pName);
    string displayText() const;
    static string connectionStateToText(CONNECTION_STATE eState);

    int serverBind();
    int acceptConnection();
    void resetConnection();
    void tryToBind();
    void tryToAccept();
    int writeClient(char* pBuf, unsigned int siz);

    virtual void calledOnceAfterAccept(void)
    {
    }

    int m_port;
    int m_sock;
    int m_client;
    CONNECTION_STATE m_state;
    pthread_t m_thread_handle;
    char m_name[32];
};

class CBrowserConnection : public CConnection
{
public:

    void calledOnceAfterAccept(void)
    {
        // Tell the browser that what follows is an mjpeg stream
        char head[512];
        sprintf(head, "HTTP/1.0 200 OK\r\nServer: mjpeg-streamer\r\nContent-Type: multipart/x-mixed-replace;boundary=informs\r\n--informs\r\n");
        writeClient(head, strlen(head));
        fflush(NULL);
    }
};
